//
//  RecoummendKJNVC.m
//  AroundTheWorld
//
//  Created by IfelseGo on 16/5/23.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "RecoummendKJNVC.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "DataHandler.h"
#import "RecoummendKJNModel.h"
#import "RecommendKJNCollectionCell.h"
#import "RecoummendKJNMDataGuidesodel.h"
#import "RecoummendKJNReusableView.h"

//静态变量 标识符
static NSString *const kjnReuse = @"kjnReuse";
static NSString *const kjnCollReuse = @"kjnCollReuse";

@interface RecoummendKJNVC ()

@end

@implementation RecoummendKJNVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //初始值
    self.kjnId = @"999999";
    [self getData];
    [self getData2];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 60)];
    view.backgroundColor = [UIColor colorWithRed:66 /255.0 green:168 / 255.0 blue:115 / 255.0 alpha:1.0];
    [self.view addSubview:view];
    
    self.myTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, 95, HEIGHT - 60) style:UITableViewStylePlain];
    self.myTabelView.backgroundColor = [UIColor whiteColor];
    self.myTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTabelView];
    
    //成为代理人
    self.myTabelView.delegate = self;
    self.myTabelView.dataSource = self;
    
    //注册
    [self.myTabelView registerClass:[UITableViewCell class] forCellReuseIdentifier:kjnReuse];
    
    //UICollectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(130, 190);
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 15, 20, 20);
    UICollectionView *collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(100, 60, WIDTH - 100, HEIGHT - 100) collectionViewLayout:flowLayout];
    
    collectionV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionV];
    
    //成为代理人
    collectionV.delegate = self;
    collectionV.dataSource = self;
    self.myCollectionV = collectionV;
    
    //注册
    UINib *nib = [UINib nibWithNibName:@"RecommendKJNCollectionCell" bundle:[NSBundle mainBundle]];
    [self.myCollectionV registerNib:nib forCellWithReuseIdentifier:kjnCollReuse];
    
    //添加返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(18, 30, 20, 20);
    button.backgroundColor = [UIColor clearColor];
    [button setBackgroundImage:[UIImage imageNamed:@"icon-fanhui 64x64"] forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonAtcion) forControlEvents:UIControlEventTouchUpInside];

}
- (void)buttonAtcion{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}


//data
-(void)getData{
    self.myKJNArray = [NSMutableArray array];
    DataHandler *handler1 = [[DataHandler alloc] init];
    [handler1 getRecoummendKJNNotes: ^(id result) {
        self.HUB = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [self.HUB show:YES];
        self.HUB.labelText = @"loading...";
    self.myKJNArray = [NSMutableArray arrayWithArray:result];
        [MBProgressHUD hideHUDForView:self.view animated:YES]; //菊花条
    [self.myTabelView reloadData];
    
    NSIndexPath *firstRow = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.myTabelView selectRowAtIndexPath:firstRow animated:NO scrollPosition:UITableViewScrollPositionTop];

    } failedHandler:^(NSError *error) {
    }];
}

- (void)getData2
{
    self.myKJNDataArray = [NSMutableArray array];
    DataHandler *handler2 = [[DataHandler alloc] init];
    [handler2 getRecoummendKJNDataNotes:self.kjnId SucceedHandler: ^(id result) {
        
        self.HUB = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [self.HUB show:YES];
        self.HUB.labelText = @"loading...";
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:result];
        RecoummendKJNMDataodel *model = [array objectAtIndex:0];
        self.myKJNDataArray = model.guides;
        
        [MBProgressHUD hideHUDForView:self.view animated:YES]; //菊花条
        
        //刷新画面
        [self.myCollectionV reloadData];
    } failedHandler:^(NSError *error) {
    }];
}

//cell高
- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

//cell数
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myKJNArray.count;
}

//建Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kjnReuse];
    RecoummendKJNModel *model = [self.myKJNArray objectAtIndex:indexPath.row];
    cell.textLabel.text =model.cnname;
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.backgroundColor = [UIColor colorWithRed:231 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:0.5];
    //点击cell字体颜色
    cell.textLabel.highlightedTextColor = [UIColor colorWithRed:75 / 255.0 green:197 / 255.0 blue:143 / 255.0 alpha:1.0];
    //点击cell背景颜色
    
    
//    self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
 
    cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
//    self.fgxLabel.backgroundColor = [UIColor colorWithRed:235 / 255.0 green:232 / 255.0 blue:232 / 255.0 alpha:1.0];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecoummendKJNModel *model = [self.myKJNArray objectAtIndex:indexPath.row];
    self.kjnId = model.idKJN;
    [self getData2];
}

//CollectionView
//cell数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.myKJNDataArray.count;
}
//建cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RecommendKJNCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kjnCollReuse forIndexPath:indexPath];
    RecoummendKJNMDataGuidesodel *model = [self.myKJNDataArray objectAtIndex:indexPath.row];

    NSString *url = [NSString stringWithFormat:@"%@/140_210.jpg?cover_updatetime%@", model.cover, model.update_time];
    [cell.KJNImageV sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
